<?php
class AdAdminAction extends AdMenuAbstract {

	/**
	 * 增加/修改管理员页
	 */
	public function doAddpage(){
		
		// 登录账号为超级管理员查询系统所有角色信息，否则查询该管理员拥有的角色列表
		$roleModule = new QudaoUserRoleModule();
		$allRole = $roleModule->getSelectRole();
		if($this->userinfo['level'] != 1){
			foreach($allRole as $key => $v){
				if(! in_array($key, $this->userinfo['role'])){
					unset($allRole[$key]);
				}
			}
		}
		
		$pageType = 1;
		if(AppCommon::intParse(AppCommon::getRequestString("adminid"), $adminid) && $adminid > 0){
			$adminModule = new QudaoUserAdminModule();
			$adminInfo = $adminModule->getUserInfoByUserID($adminid, 'id as adminid,account as name,account,level');
			
			if(empty($adminInfo)){
				PageFormat::send(false, '出错了,请重试!', "?r=home");
			}
			
			// 获取管理员角色信息
			$adminRole = $adminModule->getAdminRole($adminid);

			$pageType = 2;
			$this->smarty->assign('adminid', $adminid);
			$this->smarty->assign('adminRole', $adminRole);
			$this->smarty->assign('formaction', "?g=menu&r=adAdmin/editadmin");
			$this->smarty->assign('pagetype', $pageType);
			$this->smarty->assign('account', $adminInfo['account']);
			$this->smarty->assign('name', $adminInfo['name']);
			$this->smarty->assign('lv', $adminInfo['level']);
		}else{
			$this->smarty->assign('adminid', '');
			$this->smarty->assign('adminRole', array());
			$this->smarty->assign('pagetype', $pageType);
			$this->smarty->assign('formaction', "?g=menu&r=adAdmin/addadmin");
			$this->smarty->assign('account', '');
			$this->smarty->assign('name', '');
			$this->smarty->assign('lv', 2);
		}
		
		$this->smarty->assign('allRole', $allRole);
		$this->smarty->display('admin/add_admin.html');
	}

	/**
	 * 添加管理员列表
	 * @return void | html
	 */
	public function doAdminlist(){
        $adminModule = new BmAdminModule();

        $adminList = $adminModule->getAllUser(0, 10000, '');
        foreach ($adminList as &$val){
            $val['role_name'] = $val['level'] == 1 ? '管理员' : '普通账号';
        }
        unset($val);

		$this->smarty->assign('adminList', $adminList);
		$this->smarty->display('admin/admin_list.html');
	}

	public function doRolepage(){
		// 登录账号为超级管理员查询系统所有角色信息，否则查询该管理员拥有的角色列表
		$roleModule = new RoleModule();
		$allRole = $roleModule->getSelectRole();
		
		$roleList = array();
		foreach($allRole as $key => $v){
			$roleList[] = $v['role_id'];
			if($this->userinfo['userRole'] != UserUtil::ROLE_1 && ! in_array($key, $this->userinfo['defaultrole'])){
				unset($allRole[$key]);
			}
		}
		
		if(AppCommon::intParse(AppCommon::getRequestString("adminid"), $adminid) && $adminid > 0){
			// 获取管理员角色信息
			$adminModule = new BmAdminModule();
			$adminRole = $adminModule->getUserInfoByAdminid($adminid);
            $adminRole = $adminRole['defaultrole'] ? unserialize($adminRole['defaultrole']) : array();
            $adminRole = is_array($adminRole) ? $adminRole : array();
			
			$this->smarty->assign('adminid', $adminid);
			$this->smarty->assign('adminRole', $adminRole);
			$this->smarty->assign('allRole', $allRole);
			$this->smarty->display('admin/admin_role.html');
		}else{
			PageFormat::send(false, '非法请求!', "?r=home");
		}
	}

	/**
	 * 管理员角色接口
	 */
	public function doAdminrole(){
		$userid = AppCommon::getRequestString("adminid", 2);
		$roleid = AppCommon::getRequestString("roleid", 2);
		
		if(! AppCommon::intParse($userid, $adminid) || $userid < 0){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adAdmin/rolepage");
		}
		
		$role = serialize($roleid);
		$adminModule = new BmAdminModule();
		$result = $adminModule->editAdminRole($adminid, $role);
		
		if($result > 0){
			PageFormat::send(false, '修改成功', "?g=menu&r=adAdmin/rolepage&adminid={$adminid}");
		}else{
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/rolepage&adminid={$adminid}");
		}
	}

	/**
	 * 权限页面
	 */
	public function doAdminpowerpage(){
		$userid = AppCommon::getRequestString('roleid');
		if(! AppCommon::intParse($userid, $roleid) || $userid < 0){
			PageFormat::send(false, '非法请求!', "?r=home");
		}
		
		$menu = self::getOrderMenu();
		
		$roleModule = new RoleModule();
		$roleInfo = $roleModule->getInfoByRoleID($roleid, 'role_id,priv');
		if(empty($roleInfo)){
			PageFormat::send(false, '不存在此用户!', "?r=home");
		}
		
		$purview = empty($roleInfo['priv']) ? array() : unserialize($roleInfo['priv']);
		
		$parentMenu = null;
		if(! empty($menu)){
			// 存在父级
			if(key_exists(0, $menu) && ! empty($menu[0])){
				$parentMenu = $menu[0];
			}
		}
		
		if(! empty($parentMenu)){
			$html = "";
			foreach($parentMenu as $p_key => $p_val){
				// 非超级管理员只能分配本人拥有的权限项
				if($this->userinfo['userRole'] != UserUtil::ROLE_1 && ! in_array($p_val['menuid'], $this->userinfo['priv'])){
					continue;
				}
				if(in_array($p_val['menuid'], $purview)){
					$checked = 'checked=""';
				}
				$checked = '';
				if(in_array($p_val['menuid'], $purview)){
					$checked = 'checked=""';
				}
				// $html .= '';
				$html .= '<div class="sub" id="div_' . $p_val['menuid'] . '" >
				<label><input type="checkbox" name="power[]" id="' . $p_val['menuid'] . '" value="' . $p_val['menuid'] . '" onclick="selectallCheckbox(this,' . $p_val['menuid'] . ');" ' . $checked . '><strong>' . $p_val['name'] . '</strong></label>';
				if(! empty($menu[$p_val['menuid']])){
					$html .= '<ul id="ul_' . $p_val['menuid'] . '" class="ul2" style="margin-left:20px">';
					
					foreach($menu[$p_val['menuid']] as $c_key => $c_val){
						// 非超级管理员只能分配本人拥有的权限项
						if($this->userinfo['userRole'] != UserUtil::ROLE_1 && ! in_array($p_val['menuid'], $this->userinfo['priv'])){
							continue;
						}
						$checked = '';
						if(in_array($c_val['menuid'], $purview)){
							$checked = 'checked=""';
						}
						$html .= '<li id="' . $c_val['menuid'] . '" style="float:left;">
						<label><input type="checkbox" name="power[]" id="' . $c_val['menuid'] . '" value="' . $c_val['menuid'] . '" ' . $checked . ' onclick="checkParentCk(' . $p_val['menuid'] . ',this);">' . $c_val['name'] . '</label>
						</li>';
					}
					$html .= '</ul>';
				}
				$html .= '</div>';
			}
			$html .= '<div class="clear"></div>';
		}
		
		$this->smarty->assign('roleid', $roleid);
		$this->smarty->assign('outplist', $html);
		
		$this->smarty->display('admin/admin_power.html');
	}

	public function doPwd(){
		$this->smarty->assign("adminid", $this->userinfo["adminid"]);
		$this->smarty->display('admin/modifypwd.html');
	}

	/**
	 * 管理员角色列表
	 */
	public function doRolelist(){
		$roleModule = new RoleModule();

		$roleList = $roleModule->getAllRole();
		
		$this->smarty->assign('roleList', $roleList);
		$this->smarty->display('admin/role_list.html');
	}

	/**
	 * 新增角色
	 */
	public function doManagerole(){
		$pageType = 1;
		if(AppCommon::intParse(AppCommon::getRequestString("roleid"), $roleid) && $roleid > 0){
			$roleModule = new RoleModule();
			$roleInfo = $roleModule->getInfoByRoleID($roleid);
			
			if(empty($roleInfo)){
				PageFormat::send(false, '出错了,请重试!', "?r=home");
			}
			
			$pageType = 2;
			$this->smarty->assign('roleid', $roleid);
			$this->smarty->assign('formaction', "?g=menu&r=adAdmin/editrole");
			$this->smarty->assign('pagetype', $pageType);
			$this->smarty->assign('role_name', $roleInfo['role_name']);
			$this->smarty->assign('desc', $roleInfo['desc']);
			$this->smarty->assign('status', $roleInfo['status']);
		}else{
			$this->smarty->assign('roleid', '');
			$this->smarty->assign('pagetype', $pageType);
			$this->smarty->assign('formaction', "?g=menu&r=adAdmin/addrole");
			$this->smarty->assign('role_name', '');
			$this->smarty->assign('desc', '');
			$this->smarty->assign('status', 1);
		}
		
		$this->smarty->display('admin/add_role.html');
	}
	
	/* ================================== 接口层START ================================ */
	
	/**
	 * 添加管理员接口
	 *
	 * @access public
	 * @return void | html
	 * @link
	 *
	 */
	public function doAddadmin(){
		exit;
		$password = AppCommon::getRequestString("password", 2);
		$repassword = AppCommon::getRequestString("repassword", 2);
		$account = AppCommon::getRequestString("account", 2);
		$name = AppCommon::getRequestString("name", 2);
		$level = AppCommon::getRequestString("level", 2);
		$admin_role = AppCommon::getRequestString("admin_role", 2);
		
		if(empty($password) || empty($repassword) || empty($account) || empty($name) || empty($admin_role)){
			PageFormat::send(false, '非法请求', "?g=menu&r=adAdmin/addpage");
		}
		
		if($password != $repassword){
			PageFormat::send(false, '两次密码输入不一致', "?g=menu&r=adAdmin/addpage");
		}
		
		if(mb_strlen($account, 'utf-8') < 6){
			PageFormat::send(false, '账号长度不小于6位', "?g=menu&r=adAdmin/addpage");
		}
		
		if(mb_strlen($password, 'utf-8') < 6){
			PageFormat::send(false, '密码长度不小于6位', "?g=menu&r=adAdmin/addpage");
		}
		
		$adminModule = new QudaoUserAdminModule();
		$adminInfo = $adminModule->getUserInfoByUserName($account);
		
		if(!empty($adminInfo)){
			PageFormat::send(false, '该管理员帐号已存在', "?g=menu&r=adAdmin/addpage");
		}
		
		$rs = $adminModule->addAdmin($name, $account, CommonUtil::encodePassword($password), $level, serialize($admin_role));
		
		if(!$rs){
			PageFormat::send(false, '操作失败', "?g=menu&r=adAdmin/addpage");
		}
		
		PageFormat::send(false, '操作成功', "?g=menu&r=adAdmin/adminlist");
	}

	/**
	 * 修改管理员接口
	 *
	 * @access public
	 * @return void | html
	 * @link
	 *
	 */
	public function doEditadmin(){
		$password = AppCommon::getRequestString("password", 2);
		$repassword = AppCommon::getRequestString("repassword", 2);
		$adminid = AppCommon::getRequestString("adminid", 2);
		$name = AppCommon::getRequestString("name", 2);
		$lv = AppCommon::getRequestString("level", 2);
		$admin_role = AppCommon::getRequestString("admin_role", 2);
		
		if(! AppCommon::intParse($adminid, $userid) || $userid <= 0 || ! AppCommon::intParse($lv, $level) || $level <= 0 || empty($password) || empty($repassword) || empty($name)){
			PageFormat::send(false, '非法请求', "?g=menu&r=adAdmin/adminlist");
		}
		
		if($password != $repassword){
			PageFormat::send(false, '两次密码输入不一致', "?g=menu&r=adAdmin/adminlist");
		}
		
		$adminModule = new QudaoUserAdminModule();
		$adminInfo = $adminModule->getUserInfoByUserID($userid, 'id as adminid,account as name,account,ad_role');
		
		if(empty($adminInfo)){
			PageFormat::send(false, '出错了,请重试', "?g=menu&r=adAdmin/adminlist");
		}

		// 过滤当前管理员没有的角色列表
		$tmp_role = array();
		if($adminInfo['ad_role']){
			$tmp_role = array_diff(unserialize($adminInfo['ad_role']), unserialize($this->userinfo['ad_role']));
		}

		$roles = array_merge($tmp_role, $admin_role);

		$rs = $adminModule->editAdmin($name, $password, $userid, $level, serialize($roles));
		if(! $rs){
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/adminlist");
		}else{
			PageFormat::send(false, '修改成功', "?g=menu&r=adAdmin/adminlist");
		}
	}

	/**
	 * 添加角色接口
	 *
	 * @access public
	 * @return void | html
	 * @link
	 *
	 */
	public function doAddrole(){
		$role_name = AppCommon::getRequestString("role_name", 2);
		$desc = AppCommon::getRequestString("desc", 2);
		$status = AppCommon::getRequestInt("status", 2);
		
		if(empty($role_name)){
			PageFormat::send(false, '非法请求', "?g=menu&r=adAdmin/managerole");
		}
		
		$roleModule = new RoleModule();
		$roleInfo = $roleModule->getInfoByRoleName($role_name);
		
		if(! empty($roleInfo)){
			PageFormat::send(false, '该角色已存在', "?g=menu&r=adAdmin/managerole");
		}

		$rs = $roleModule->addRole($role_name, $desc, $status, $this->userinfo['userid']);
		if(! $rs){
			PageFormat::send(false, '添加失败', "?g=menu&r=adAdmin/managerole");
		}
		
		PageFormat::send(false, '添加成功', "?g=menu&r=adAdmin/rolelist");
	}

	/**
	 * 修改角色接口
	 *
	 * @access public
	 * @return void | html
	 * @link
	 *
	 */
	public function doEditrole(){
		$roleid = AppCommon::getRequestInt("roleid", 2);
		$role_name = AppCommon::getRequestString("role_name", 2);
		$desc = AppCommon::getRequestString("desc", 2);
		$status = AppCommon::getRequestInt("status", 2);
		
		if(! AppCommon::intParse($roleid, $userid) || $userid <= 0 || empty($role_name)){
			PageFormat::send(false, '非法请求', "?g=menu&r=adAdmin/rolelist");
		}
		
		$roleModule = new RoleModule();
		$roleInfo = $roleModule->getInfoByRoleID($roleid);
		
		if(empty($roleInfo)){
			PageFormat::send(false, '出错了,请重试', "?g=menu&r=adAdmin/rolelist");
		}
		
		$rs = $roleModule->editRole($userid, $role_name, $desc, $status);
		if(! $rs){
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/rolelist");
		}else{
			PageFormat::send(false, '修改成功', "?g=menu&r=adAdmin/rolelist");
		}
	}

	/**
	 * 权限接口
	 */
	public function doRolepower(){
		$userid = AppCommon::getRequestString("roleid", 2);
		$purview = AppCommon::getRequestString("power", 2);
		
		if(! AppCommon::intParse($userid, $roleid) || $userid < 0){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adAdmin/adminpowerpage");
		}
		
		$purview = serialize($purview);
		$roleModule = new RoleModule();
		
		$result = $roleModule->editRolePower($roleid, $purview);
		
		if($result > 0){
			PageFormat::send(false, '修改成功', "?g=menu&r=adAdmin/adminpowerpage&roleid={$roleid}");
		}else{
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/adminpowerpage&roleid={$roleid}");
		}
	}

	/**
	 * 删除角色
	 */
	public function doDelrole(){
		$roleid = AppCommon::getRequestInt("roleid", 1);
		
		if($roleid <= 0){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adAdmin/rolelist");
		}
		
		$roleModule = new AdUserRoleModule();
		$result = $roleModule->deleteRole($roleid);
		
		if($result > 0){
			PageFormat::send(false, '修改成功', "?g=menu&r=adAdmin/rolelist");
		}else{
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/rolelist");
		}
	}

	/**
	 * 删除管理员
	 */
	public function doDelAdmin(){
		exit;
		$userid = AppCommon::getRequestInt("adminid", 1);
		
		if($userid <= 0){
			PageFormat::send(false, '非法请求!', "?g=menu&r=adAdmin/adminlist");
		}
		if($userid == 1){
			PageFormat::send(false, '系统默认超级管理员不能被删除！', "?g=menu&r=adAdmin/adminlist");
		}
		
		$adminModule = new QudaoUserAdminModule();
		$result = $adminModule->deleteAdmin($userid);
		
		if($result > 0){
			PageFormat::send(false, '删除成功', "?g=menu&r=adAdmin/adminlist");
		}else{
			PageFormat::send(false, '删除失败', "?g=menu&r=adAdmin/adminlist");
		}
	}

	/**
	 * 修改密码
	 */
	public function doChangepwd(){
		$userinfo = Common::getUserInfo();
		$userid = $userinfo["adminid"];
		
		if($userid <= 0){
			PageFormat::send(false, '非法请求', "?g=menu&r=adAdmin/pwd");
		}
		
		$oldpwd = AppCommon::getRequestString("oldpwd", 2);
		$newpwd = AppCommon::getRequestString("newpwd", 2);
		$repwd = AppCommon::getRequestString("repwd", 2);
		
		if(empty($oldpwd) || empty($newpwd) || empty($repwd)){
			PageFormat::send(false, '请输入完整信息', "?g=menu&r=adAdmin/pwd");
		}
		
		if($oldpwd == $newpwd){
			PageFormat::send(false, '新密码和旧密码不能相同！', "?g=menu&r=adAdmin/pwd");
		}
		
		if($repwd != $newpwd){
			PageFormat::send(false, '新密码和重复密码不一致！', "?g=menu&r=adAdmin/pwd");
		}
		
		$adminModule = new QudaoUserAdminModule();
		$result = $adminModule->changePwd($userid, $oldpwd, $newpwd);
		
		if($result > 0){
			Common::clearLoginInfo();
			PageFormat::send(false, '修改成功', "?r=home");
		}else{
			PageFormat::send(false, '修改失败', "?g=menu&r=adAdmin/pwd");
		}
	}
}
